﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ServiceModel;

namespace DMSClient.Distributors
{
    public partial class Cabinet : Form
    {
        
        private DataTable dtVisit;
        private string ambiente;
        private WSHttpBinding Binding;
        private EndpointAddress address;

        public Cabinet()
        {
            InitializeComponent();

            Binding = new WSHttpBinding();
            Binding.MaxReceivedMessageSize = Int32.MaxValue;
            Binding.MaxBufferPoolSize = Int32.MaxValue;
            Binding.CloseTimeout = TimeSpan.MaxValue;
            Binding.ReceiveTimeout = TimeSpan.MaxValue;
            Binding.SendTimeout = TimeSpan.MaxValue;
            Binding.Security.Mode = SecurityMode.None;
            Binding.ReaderQuotas.MaxStringContentLength = Int32.MaxValue;

            address = new EndpointAddress(Properties.Settings.Default.Server);
            this.ambiente = Properties.Settings.Default.CurrentEnv;

            this.Text = Properties.Settings.Default.CurrentEnvDes + " - Cabinet Inventory Report";

            uceDistributors.DisplayMember = "cdOrgUnit";
            uceDistributors.ValueMember = "idOrgUnit";
            string distributors = "";
            DMSServices.DMSServicesClient client = new DMSServices.DMSServicesClient(Binding, address);

            try
            {
                client.Open();
                distributors = client.getDistributorsList(ambiente);
                client.Close();
            }
            catch (Exception ex)
            {                 
                throw new Exception(Properties.Settings.Default.DistributorsListErrorMsg, ex.InnerException);                
            }
            if (distributors == null)
            {
                throw new Exception(Properties.Settings.Default.DistributorsNullListErrorMsg);
            }


            DataTable dt = new DataTable("dtDistributorsList");
            dt.Columns.Add("idOrgUnit");
            dt.Columns.Add("cdOrgUnit");


            DataSet ds = new DataSet();
            ds.Tables.Add(dt);

            System.IO.StringReader xmlSR = new System.IO.StringReader(distributors);
            ds.ReadXml(xmlSR, XmlReadMode.ReadSchema);
            uceDistributors.DataSource = ds.Tables[0];
            uceDistributors.SelectedIndex = 0;

            xmlSR.Close();
            ugGridVisit.DisplayLayout.Bands[0].Columns.Add("cdCabinet", "cdCabinet");
            ugGridVisit.DisplayLayout.Bands[0].Columns.Add("cdStore", "cdStore");
            ugGridVisit.DisplayLayout.Bands[0].Columns.Add("nrEqupiment", "nrEqupiment");
            ugGridVisit.DisplayLayout.Bands[0].Columns.Add("cdStatus", "cdStatus");
            ugGridVisit.DisplayLayout.Bands[0].Columns.Add("dtInventory", "dtInventory");
        }

        private void ultraButton1_Click(object sender, EventArgs e)
        {

            WorkerCabinet.RunWorkerAsync();
            estado(false);
                  
             
        }

        

        private void Cabinet_Load(object sender, EventArgs e)
        {
           
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
           
        }
        private void closeThis()
        {
            this.Close();
        }

        private void WorkerCabinet_DoWork(object sender, DoWorkEventArgs e)
        {




            DMSServices.DMSServicesClient client = new DMSServices.DMSServicesClient(Binding, address);
            string lista = client.getCabinetInventoryReport(uceDistributors.Value.ToString(), ambiente);
            dtVisit = new DataTable("Cabinetdt");
            dtVisit.Columns.Add("cdCabinet");
            dtVisit.Columns.Add("cdStore");
            dtVisit.Columns.Add("nrEqupiment");
            dtVisit.Columns.Add("cdStatus");
            dtVisit.Columns.Add("dtInventory");
            DataSet ds = new DataSet();
            ds.Tables.Add(dtVisit);
            System.IO.StringReader xmlSR = new System.IO.StringReader(lista);
            ds.ReadXml(xmlSR, XmlReadMode.ReadSchema);           
            xmlSR.Close();
        }

        private void WorkerCabinet_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                if (!this.IsDisposed)
                {
                    this.BringToFront();
                    MessageBox.Show(this, e.Error.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    estado(true);
                }
            }
            else if (!e.Cancelled && !this.IsDisposed)
            {
                ugGridVisit.DataSource = dtVisit;
                estado(true);
            }
        }

        public void estado(bool estado) 
        {
            uceDistributors.Enabled = estado;
            Consultar.Enabled = estado;
           // Tab.Controls[2].Enabled = estado; el ame
            loader.Visible = !estado;
            Export.Enabled = estado;
           
            
        }

        private void ultraButton1_Click_1(object sender, EventArgs e)
        {
            if (ugGridVisit.Rows.Count < 1)
            {
                MessageBox.Show(this, "There is no data to export", "Visit Report", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            System.Windows.Forms.SaveFileDialog dialogo = new System.Windows.Forms.SaveFileDialog();
            dialogo.DefaultExt = "xls";
            Infragistics.Win.UltraWinGrid.ExcelExport.UltraGridExcelExporter Exporter = new Infragistics.Win.UltraWinGrid.ExcelExport.UltraGridExcelExporter();
            if (dialogo.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Exporter.Export(ugGridVisit, dialogo.FileName);
                if (MessageBox.Show(this, "¿Desea abrir el archivo?", "Cabinet Report", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    System.Diagnostics.Process.Start(dialogo.FileName);
                }
            }
        }

        private void Cabinet_Shown(object sender, EventArgs e)
        {
            loader.Left = (this.Width - loader.Width) / 2;
            loader.Top = ((this.Height - loader.Height) / 2) - loader.Height;
        }

        private void Cabinet_Resize(object sender, EventArgs e)
        {
            loader.Left = (this.Width - loader.Width) / 2;
            loader.Top = ((this.Height - loader.Height) / 2) - loader.Height;
        }

        private void Cabinet_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (WorkerCabinet.IsBusy)
            {
                WorkerCabinet.CancelAsync();
            }
        }
    }

    
}
